package day4;

import javax.xml.crypto.Data;
import java.lang.reflect.Field;
import java.util.Date;

//测试反射
public class Persion extends Animal {
    private Integer id;
    String name;
    protected double money;
    public Date birthday;

    public Persion() {
        System.out.println("无参构造");
    }

    private Persion(Integer id) {
        this.id = id;
        System.out.println("有参构造");
    }

    private static void test1() {
        System.out.println("test1");
    }

    public static void test2(String name, Integer id) {
        System.out.println("test2");
    }

    public static int test3(int a, int b) {
        System.out.println("求和为: ");
        return a+b;
    }

    public static void main(String[] args) {
        //1.获取类对象
        Persion p = new Persion();
        Class<?> c = p.getClass();

        //2.通过类对象调用getXXX() 获取类的资源
        String name = c.getName();
        System.out.println("全类名:" + name);
        String simpleName = c.getSimpleName();
        System.out.println("类名:" + simpleName);
        String superName = c.getSuperclass().getSuperclass().getName();
        System.out.println("超类:" + superName);
        System.out.println("------------获取属性Field------------");
        //获取类中的所有属性，但是不包括父类
        Field[] fs = c.getDeclaredFields();
        for (Field f : fs) {
            String fieldName = f.getName();
            Class type = f.getType();
            System.out.println("属性名:" + fieldName + "类型:" + type);
        }

        System.out.println("------------------------");
        //只能获取类中的公开资源（自己和父类的...）
        Field[] fs2 = c.getFields();
        for (Field f : fs2) {
            String fieldName = f.getName();
            Class type = f.getType();
            System.out.println("属性名:" + fieldName + "类型:" + type);
        }
    }
}

class Animal {
    private int height;
    int age;
    protected String sex;
    public String addr;
}
